package com.jt9003.controller;

import com.jt9003.pojo.DrugsExport;
import com.jt9003.pojo.StockInquiry;
import com.jt9003.service.DrugsIputService;
import com.jt9003.utils.PageUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/DrugsInputController")
public class DrugsInputController {
    @Resource
    private DrugsIputService drugsIputService;

    @RequestMapping("/updateStatus")
    @ResponseBody
    public Object  updateStatus( @RequestParam(value = "documentNo", required = false) String documentNo,
                                 @RequestParam(value = "drName", required = false) String drName
    ){
        Map<String , Object> map=new HashMap<String , Object>();
        Map<String , Object> param=new HashMap<String , Object>();
        try {
            DrugsExport drugsExport =drugsIputService.query(documentNo);
            String drugsName=drName;
            System.out.print(drugsName+"===============================================");
            Integer inventoryNumber=drugsExport.getNumber();
            Integer warehouseUnit1=drugsExport.getCompany();
            String warehouseUnit;
            if(warehouseUnit1==34){
                warehouseUnit="盒";
            }else if(warehouseUnit1==35){
                warehouseUnit="袋";
            }else{
                warehouseUnit=null;
            }

            param.put("drugsName",drugsName);
            param.put("inventoryNumber",inventoryNumber);
            param.put("warehouseUnit",warehouseUnit);

            Integer  num = drugsIputService.updateStatus(documentNo);
            if (num>0) {
                StockInquiry stockInquiry =drugsIputService.isDrName(param);
                System.out.print((stockInquiry==null)+"===============================================");
                if(stockInquiry==null){
                    drugsIputService.add(param);
                }else{
                   Integer count =drugsIputService.queryInventoryNumber(param);
                    Integer inventoryNumber2=count+inventoryNumber;
                    param.put("inventoryNumber2",inventoryNumber2);
                    drugsIputService.updateInventoryNumber(param);
                }
                map.put("msg", "入库成功");
                map.put("success", true);
            } else {
                map.put("msg", "入库失败");
                map.put("success", false);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }
}
